﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Net.Chirash.Twitter.TwitterClient;
using Net.Chirash.Twitter.TwitterDatabase;

namespace Net.Chirash.Twitter.TweetZone.WindowComponents.ViewModels
{
    public abstract class PagableContainerViewModel<T>
        : TwitterContainerViewModel<T>
        where T : TwitterData
    {
        public PagableContainerViewModel()
            :base()
        {
        }

        public PagableContainerViewModel(User user)
            : base(user)
        {
        }

        protected abstract void PagingDataProviding(PagingEventArgs<T> e);

        protected override RefreshArgs<T> CreateRefreshArgs(RefreshArgs<T> args)
        {
            PagingEventArgs<T> result = args as PagingEventArgs<T>;
            if (args == null)
                result = new PagingEventArgs<T>();

            base.CreateRefreshArgs(result);
            if(this.Collection != null && this.Collection.Count > 0) 
                result.LastData = this.Collection.Last();

            return result;
        }

        public void PagingStart()
        {
            if (Collection == null)
                return;

            lock (LockIsRefreshingColumn)
            {
                if (this.IsRefreshingColumn)
                    return;
            }

            var args = CreateRefreshArgs(null);
            args.IsAsyncCall = true;

            Task.Factory.StartNew(() =>
                {
                    lock (LockIsRefreshingColumn)
                        this.IsRefreshingColumn = true;

                    PagingDataProviding(args as PagingEventArgs<T>);
                }
            ).ContinueWith((task) =>
                {
                    try
                    {
                        if (task.Exception != null)
                            LastErrorMessage.Provider = new ErrorMessageProvider(task.Exception);
                        else
                        {
                            if (args.ResultEnumerable != null)
                            {
                                foreach (var item in args.ResultEnumerable)
                                    this.Collection.Add(item);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        lock (LockIsRefreshingColumn)
                        {
                            this.IsRefreshingColumn = false;
                        }
                    }
                }, TaskScheduler.FromCurrentSynchronizationContext()
            ); //end task
        }


    }

    public class PagingEventArgs<T>
        : RefreshArgs<T>
        where T : TwitterData
    {
        public T LastData;
    }

}
